package com.learning.railwayreservation.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import com.learning.railwayreservation.vo.CheckTicketsAvailibiltyInputVO;
import com.learning.railwayreservation.vo.AvailabilityDetailsVo;
import com.learning.railwayreservation.vo.SeatVO;

public class CheckTicketsAvailibiltyDao extends BaseDao {
	public static void main(String[] args) {

	}

	
	public AvailabilityDetailsVo getSeatAvailDetails(CheckTicketsAvailibiltyInputVO test2Vo) {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		CheckTicketsAvailibiltyDao tdao = new CheckTicketsAvailibiltyDao();
		Connection conn = tdao.getConnection();
		//List<AvailabilityDetailsVo> testList = new ArrayList<AvailabilityDetailsVo>();
		SeatVO seatVo;
		List<SeatVO> seatList = new ArrayList<SeatVO>();
		
		AvailabilityDetailsVo availabilityDetailsVo = new AvailabilityDetailsVo();
		try {

			CallableStatement stmt = conn
					.prepareCall("BEGIN railway_reservation.check_ticket_availability( ?, ?, ?, ?, ?); END;");

			
			stmt.setInt(1, test2Vo.getTrainId());
			stmt.setString(2, test2Vo.getBoggieType());
			stmt.setInt(3, test2Vo.getRequiredSeats());
			stmt.setDate(4, Date.valueOf(test2Vo.getStartDate()));

			stmt.registerOutParameter(5, OracleTypes.CURSOR); // REF CURSOR
			stmt.execute();
			ResultSet rs = (ResultSet) ((OracleCallableStatement) stmt)
					.getObject(5);

			while (rs.next()) {
				seatVo = new SeatVO();
				availabilityDetailsVo.setTrainName(rs.getString("TRAIN_NAME"));
//				availabilityDetailsVo.setTrainId(rs.getInt("TRAIN_ID"));
//				availabilityDetailsVo.setStartDate(rs.getString("START_DATE"));
//				availabilityDetailsVo.setStartStation(rs.getString("START_STATION"));
//				availabilityDetailsVo.setEndStation(rs.getString("END_STATION"));
				seatVo.setSeatId(rs.getInt("SEAT_ID"));
				seatVo.setUniqueSeatId(rs.getInt("S_ID"));
				seatVo.setRowId(rs.getString("ROW_ID"));
				seatVo.setBoggieId(rs.getInt("B_ID"));
				seatList.add(seatVo);
			}
			availabilityDetailsVo.setSeatList(seatList);
			// System.out.println(passengerName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return availabilityDetailsVo;
	}

}